package com.base.cn.platform.os.service.course.count;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.course.count.CouMaterialPlayNumDao;
import com.base.cn.platform.os.entity.course.catalog.CourseCatalog;
import com.base.cn.platform.os.entity.course.count.CouMaterialPlayNum;
import com.base.cn.platform.os.entity.course.material.CouMaterial;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogBiz;
import com.base.cn.platform.os.service.course.material.CouMaterialBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 素材播放量统计Biz
 *
 * @author s.li
 * @date 2019/04/15
 */
@Service
public class CouMaterialPlayNumBiz extends BaseBiz<CouMaterialPlayNum, CouMaterialPlayNumDao> {

    @Autowired
    private CourseCatalogBiz courseCatalogBiz;
    @Autowired
    private CouMaterialBiz couMaterialBiz;
    @Autowired
    private CourseSubjectPlayNumBiz courseSubjectPlayNumBiz;
    /**
     * 创建素材播放量
     * @param catalogId 目录ID
     */
    @Async
    public void createMaterialPlayNum(BigDecimal catalogId){
        CourseCatalog courseCatalog = courseCatalogBiz.findById(catalogId);
        if(courseCatalog!=null){
            CouMaterial material = couMaterialBiz.findById(courseCatalog.getMaterialId());
            if(ObjectUtils.isNotEmpty(material)){
                CouMaterialPlayNum materialPlayNum = this.findOne(" materialId="+material.getId(),null);
                if(ObjectUtils.isNotEmpty(materialPlayNum)){
                    materialPlayNum.setPlayCount(materialPlayNum.getPlayCount().intValue() + 1);
                    this.updateById(materialPlayNum,null);
                }else{
                    materialPlayNum = new CouMaterialPlayNum();
                    materialPlayNum.setMaterialId(material.getId());
                    materialPlayNum.setPlayCount(1);
                    materialPlayNum.setTypeKey(material.getTypeKey());
                    this.save(materialPlayNum);
                }
                String subjectIds = material.getSubjectIds();
                courseSubjectPlayNumBiz.createCourseSubjectPlayNum(subjectIds);
            }
        }
    }
}
